from tornado import ioloop, web
import json
# from handler.security.login import LoginHandler


class StopServerHandler(web.RequestHandler):
    def initialize(self, password):
        self.password = password

    def get(self):
        pwd = self.get_argument('pwd', None)
        result = False
        if pwd is not None:
            if pwd==self.password:
                current = ioloop.IOLoop.current(False)
                if current is not None:
                    current.stop()
                    msg = "Python web server is stopped"
                    result = True
                else:
                    msg = "Python web server is not running"
            else:
                msg = "You have no permission to stop the web server."

        self.set_header("Content-type", "application/json")
        self.write(json.dumps({"result": result, "msg":msg}))

